package com.kuang.config;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

//这个是个拦截器，拦截器是用于查看各个url是否符合特定的标准的，例如是否有session
public class LoginHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //return HandlerInterceptor.super.preHandle(request, response, handler);

        Object loginUser=request.getSession().getAttribute("loginUser");
        System.out.println("this is loginUser:"+loginUser+",url is:http://"+request.getServerName()+":"+request.getServerPort()+request.getRequestURI()+".");
        if (loginUser==null){
            request.setAttribute("msg","没有权限，请先登录");
            request.getRequestDispatcher("/login").forward(request,response);
        }
        return false;
    }
}
